package br.com.intelsys.basics.spring.validator;

import java.math.BigDecimal;

import org.apache.commons.validator.GenericValidator;

import br.com.intelsys.basics.utils.FieldMaskMaker;

public abstract class GenericIValidator extends IValidator {

	public void addAccessDenidedError() throws IValidatorException {
		checkIValidatorException(addError(null, "genericError.accessDenided"));
	}

	public void addUnknowError(Throwable e) throws IValidatorException {
		super.addUnknowError(e);
		throw new IValidatorException("genericError.unknowError");
	}

	public void addUnknowError(Throwable e, Object entity) throws IValidatorException {
		super.addUnknowError(e);
		IValidatorException ie = new IValidatorException("genericError.unknowError");
		ie.setEntity(entity);
		throw ie;
	}

	protected IValidatorException validateTextField(IValidatorException iValidatorException, String name, String value) {
		return validateTextField(iValidatorException, name, value, -1, -1);
	}

	protected IValidatorException validateRequiredTextField(IValidatorException iValidatorException, String name, String value) {
		return validateTextField(iValidatorException, name, value, 1, -1);
	}

	protected IValidatorException validateTextField(IValidatorException iValidatorException, String name, String value, int maxValue) {
		return validateTextField(iValidatorException, name, value, -1, maxValue);
	}

	protected IValidatorException validateRequiredTextField(IValidatorException iValidatorException, String name, String value, int maxValue) {
		return validateTextField(iValidatorException, name, value, 1, maxValue);
	}

	protected IValidatorException validateMoneyField(IValidatorException iValidatorException, String name, String value) {
		if (FieldMaskMaker.getMoneyFromMask(value) == null) {
			iValidatorException = addError(iValidatorException, "genericError.invalidMoney", name);
		}
		return iValidatorException;
	}

	protected IValidatorException validateTextField(IValidatorException iValidatorException, String name, String value, int minValue,
			int maxValue) {
		int valueLength = value != null ? value.length() : 0;
		if (minValue > 0 && (value == null || valueLength < minValue)) {
			if (minValue == maxValue) {
				iValidatorException = addError(iValidatorException, "genericError.minMaxFixLengthField", name, Integer.toString(minValue));
			} else if (minValue == 1) {
				iValidatorException = addError(iValidatorException, "genericError.requiredField", name);
			} else {
				iValidatorException = addError(iValidatorException, "genericError.minLengthField", name, Integer.toString(minValue),
						Integer.toString(valueLength));
			}
		} else if (maxValue > 0 && value != null && valueLength > maxValue) {
			iValidatorException = addError(iValidatorException, minValue == maxValue ? "genericError.minMaxFixLengthField"
					: "genericError.maxLengthField", name, Integer.toString(maxValue), Integer.toString(valueLength));
		}
		return iValidatorException;
	}

	protected IValidatorException validateEmail(IValidatorException iValidatorException, String email) {
		return validateEmail(iValidatorException, "genericFieldName.email", email);
	}

	protected IValidatorException validateUrl(IValidatorException iValidatorException, String site) {
		return validateUrl(iValidatorException, "genericFieldName.site", site);
	}

	protected IValidatorException validateUrl(IValidatorException iValidatorException, String site, boolean withProtocol) {
		return validateUrl(iValidatorException, "genericFieldName.site", site, withProtocol);
	}

	protected IValidatorException validateUrl(IValidatorException iValidatorException, String name, String site) {
		return validateUrl(iValidatorException, name, site, false);
	}

	protected IValidatorException validateUrl(IValidatorException iValidatorException, String name, String site, boolean withProtocol) {
		if (GenericValidator.isBlankOrNull(site)) {
			iValidatorException = addError(iValidatorException, "genericError.requiredField", name);
		} else if (!GenericValidator.isUrl(withProtocol ? site : "http://" + site)) {
			iValidatorException = addError(iValidatorException, "genericError.invalidSite", name);
		}
		return iValidatorException;
	}

	protected IValidatorException validateEmail(IValidatorException iValidatorException, String name, String email) {
		if (GenericValidator.isBlankOrNull(email)) {
			iValidatorException = addError(iValidatorException, "genericError.requiredField", name);
		} else if (!GenericValidator.isEmail(email)) {
			iValidatorException = addError(iValidatorException, "genericError.invalidEmail", name);
		}
		return iValidatorException;
	}

	protected IValidatorException validateEmailOptional(IValidatorException iValidatorException, String email) {
		return validateEmailOptional(iValidatorException, "genericFieldName.email", email);
	}

	protected IValidatorException validateEmailOptional(IValidatorException iValidatorException, String name, String email) {
		if (GenericValidator.isBlankOrNull(email)) {
			return iValidatorException;
		} else if (!GenericValidator.isEmail(email)) {
			iValidatorException = addError(iValidatorException, "genericError.invalidEmail", name);
		}
		return iValidatorException;
	}

	protected IValidatorException validateNumber(IValidatorException iValidatorException, String name, Object value) {
		String valueStr = value != null ? value.toString() : null;
		if (valueStr != null
				&& !(GenericValidator.isInt(valueStr) || GenericValidator.isDouble(valueStr) || GenericValidator.isDouble(valueStr)
						|| GenericValidator.isLong(valueStr) || GenericValidator.isShort(valueStr))) {
			return addError(iValidatorException, "genericError.numberInvalid", name);
		}
		return iValidatorException;
	}

	protected IValidatorException validateNumber(IValidatorException iValidatorException, String name, Integer value, Integer maxValue) {
		return validateNumber(iValidatorException, name, value, null, maxValue);
	}

	protected IValidatorException validateNumber(IValidatorException iValidatorException, String name, Integer value, Integer minValue,
			Integer maxValue) {
		if (value == null) {
			iValidatorException = addError(iValidatorException, "genericError.numberInvalid", name);
		} else if (minValue != null && maxValue != null && !GenericValidator.isInRange(value, minValue, maxValue)) {
			iValidatorException = addError(iValidatorException, "genericError.numberIsNotInRange", name, minValue.toString(),
					maxValue.toString());
		} else if (minValue != null && value < minValue) {
			iValidatorException = addError(iValidatorException, "genericError.numberMinThanRange", name, minValue.toString());
		} else if (maxValue != null && value > maxValue) {
			iValidatorException = addError(iValidatorException, "genericError.numberMaxThanRange", name, maxValue.toString());
		}
		return iValidatorException;
	}

	protected IValidatorException validateNumber(IValidatorException iValidatorException, String name, Double value, Double maxValue) {
		return validateNumber(iValidatorException, name, value, null, maxValue);
	}

	protected IValidatorException validateNumber(IValidatorException iValidatorException, String name, Float value, Double minValue,
			Double maxValue) {
		return validateNumber(iValidatorException, name, value, minValue, maxValue);
	}

	protected IValidatorException validateNumber(IValidatorException iValidatorException, String name, Double value, Double minValue,
			Double maxValue) {
		if (value == null) {
			iValidatorException = addError(iValidatorException, "genericError.numberInvalid", name);
		} else if (minValue != null && maxValue != null && !GenericValidator.isInRange(value, minValue, maxValue)) {
			BigDecimal bdMinValue = new BigDecimal(minValue);
			BigDecimal bdMaxValue = new BigDecimal(maxValue);
			bdMinValue.setScale(2);
			bdMaxValue.setScale(2);
			iValidatorException = addError(iValidatorException, "genericError.numberIsNotInRange", name, bdMinValue.toString(),
					bdMaxValue.toString());
		} else if (minValue != null && value < minValue) {
			BigDecimal bdMinValue = new BigDecimal(minValue);
			bdMinValue.setScale(2);
			iValidatorException = addError(iValidatorException, "genericError.numberMinThanRange", name, bdMinValue.toString());
		} else if (maxValue != null && value > maxValue) {
			BigDecimal bdMaxValue = new BigDecimal(maxValue);
			bdMaxValue.setScale(2);
			iValidatorException = addError(iValidatorException, "genericError.numberMaxThanRange", name, bdMaxValue.toString());
		}
		return iValidatorException;
	}

	protected IValidatorException validateCpf(IValidatorException iValidatorException, String value) {
		return validateCpf(iValidatorException, "genericFieldName.cpf", value);
	}

	protected IValidatorException validateCpf(IValidatorException iValidatorException, String name, String cpf) {
		if (!CNPValidator.isValidCPF(cpf)) {
			iValidatorException = addError(iValidatorException, "genericError.invalidField", name);
		}
		return iValidatorException;
	}

	protected IValidatorException validateRequiredCod(IValidatorException iValidatorException, String name, Integer value) {
		if (value == null || value <= 0) {
			iValidatorException = addError(iValidatorException, "genericError.invalidField", name);
		}
		return iValidatorException;
	}
	/*
		public IValidatorException validateValueOf(IValidatorException iValidatorException, String name, Class<T> clazz, Object value) {
			try {
				Enum.valueOf(clazz, value);
				return iValidatorException;
			} catch (Exception e) {
				return addError(iValidatorException, "genericError.invalidField", name);
			}
		}
	*/
}
